package com.distribute.application.api.rest.member;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.UserAddress;

@RestController(value="api.MemberAddressController")
public class MemberAddressController  extends BaseApiController{
	/**
	 * 用户获取自己地址列表
	 * @return
	 */
	@RequestMapping("/api/address/userAddressList.do")
	public @title("用户获取自己地址列表")  Protocol userAddressList(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setList(UserAddress.where.member(member.getId()).list(10), new Protocol.BeanFormat<UserAddress>(){
			@Override
			public Map format(UserAddress t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 用户获取单个地址详情
	 * @return
	 */
	@RequestMapping("/api/address/userAddressInfo.do")
	public @title("用户获取单个地址详情")  Protocol userAddressInfo(
			@title("地址编号") Long addressId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		UserAddress userAddress=UserAddress.where.id(addressId).get();
		Region region=Region.where.id(userAddress.getAddress()).get();
		if (region.getLevel()==(short)3) {
			Region region1=Region.where.id(region.getParent()).get();
			Region region2=Region.where.id(region1.getParent()).get();
			protocol.set("province",region2.toJSON() );
			protocol.set("city",region1.toJSON());
			protocol.set("area", region.toJSON());
			protocol.set("country",null);
		}else {
			Region region1=Region.where.id(region.getParent()).get();
			Region region2=Region.where.id(region1.getParent()).get();
			Region region3=Region.where.id(region2.getParent()).get();
			protocol.set("province",region3.toJSON() );
			protocol.set("city",region2.toJSON());
			protocol.set("area", region1.toJSON());
			protocol.set("country",region.toJSON());
		}
		
		
		protocol.set("address", userAddress==null?null:userAddress.toJSON());
		return protocol;
	}
	/**
	 * 获取省份列表
	 * @return
	 */
	@RequestMapping("/api/address/provinceList.do")
	public @title("获取省份列表")  Protocol provinceList(
	){
		Protocol protocol = Protocol.create(true, null);
		protocol.setList(Region.where.level((short)1).parent((long)0).list(-1), new Protocol.BeanFormat<Region>(){
			@Override
			public Map format(Region t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 根据父级获取地址信息
	 * @return
	 */
	@RequestMapping("/api/address/pidAdderssList.do")
	public @title("根据父级获取地址信息")  Protocol pidAdderssList(
			@title("父级编号") Long pid
	){
		Protocol protocol = Protocol.create(true, null);
		protocol.setList(Region.where.parent(pid).list(-1), new Protocol.BeanFormat<Region>(){
			@Override
			public Map format(Region t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 用户新赠收货地址
	 * @return
	 */
	@RequestMapping("/api/address/userSaveAddress.do")
	public @title("用户新赠收货地址")  Protocol userSaveAddress(
			@title("手机号码") String mobile,
			@title("收货姓名") String realName,
			@title("是否默认") Short isDefault,
			@title("性别") Short sex,
			@title("所属城市") Long cityId,
			@title("地址编号") Long addressId,
			@title("地址详情") String detailAddress,
			@title("经度") String longitude,
			@title("纬度") String latitude,
			@title("坐标地址") String coordinate
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		
		  if(isDefault==(short)1){
			   UserAddress userAddresses=UserAddress.where.member(member.getId()).isDefault((short)1).get();
			    if (userAddresses!=null) {
			    	 userAddresses.setIsDefault((short)0);
					   userAddresses.update();
				}
		   }
		  Region region=Region.where.id(addressId).get();
		  UserAddress userAddress=new UserAddress();
		   userAddress.setMember(member.getId());
		   userAddress.setMobile(mobile);
		   userAddress.setRealName(realName);
		   userAddress.setIsDefault(isDefault);
		   userAddress.setSex(sex);
		   userAddress.setCityOrigin(cityId);
		   userAddress.setFullAddress(region.getFullName());
		   userAddress.setAddress(addressId);
		   userAddress.setDetailAddress(detailAddress);
		   userAddress.setLongitude(longitude);
		   userAddress.setLatitude(latitude);
		   userAddress.setCoordinate(coordinate);
		   userAddress.save();
		protocol.set("msg", "添加成功");
		return protocol;
	}
	/**
	 * 用户修改收货地址
	 * @return
	 */
	@RequestMapping("/api/address/userEditAddress.do")
	public @title("用户修改收货地址")  Protocol userEditAddress(
			@title("编号") Long id,
			@title("手机号码") String mobile,
			@title("收货姓名") String realName,
			@title("是否默认") Short isDefault,
			@title("性别") Short sex,
			@title("所属城市") Long cityId,
			@title("地址编号") Long addressId,
			@title("地址详情") String detailAddress,
			@title("经度") String longitude,
			@title("纬度") String latitude,
			@title("坐标地址") String coordinate
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		UserAddress userAddress=UserAddress.where.id(id).get();
		   if(isDefault==(short)1){
			   UserAddress userAddresses=UserAddress.where.member(member.getId()).isDefault((short)1).get();
			    if (userAddresses!=null) {
			    	userAddresses.setIsDefault((short)0);
					userAddresses.update();
				}
		   }
		   Region region=Region.where.id(addressId).get();
		   userAddress.setMobile(mobile);
		   userAddress.setRealName(realName);
		   userAddress.setIsDefault(isDefault);
		   userAddress.setAddress(addressId);
		   userAddress.setFullAddress(region.getFullName());
		   userAddress.setCityOrigin(cityId);
		   userAddress.setSex(sex);
		   userAddress.setDetailAddress(detailAddress);
		   userAddress.setLongitude(longitude);
		   userAddress.setLatitude(latitude);
		   userAddress.setCoordinate(coordinate);
		   userAddress.update();
		protocol.set("msg", "修改成功");
		return protocol;
	}
}
